#include <iostream>
#include <crypt.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <shadow.h>

using std::cout;
using std::endl;

void get_salt(char *salt, char *password)
{
    int i{}, j{};
    for(;password[j] != '\0' && j < 3; i++){
        if(password[i] == '$')
            j++;
    }

    strncpy(salt, password, i - 1);
}

void test(char **argv)
{
    struct spwd *sp;
    char *pwd = getpass("pwd: "); 

    if((sp = getspnam(argv[1])) == NULL){
        perror("getspnam");
    }

    char salt[512]{};
    get_salt(salt, sp->sp_pwdp);

    cout << sp->sp_pwdp << endl;
    cout << crypt(pwd, salt) << endl;
}

int main(int args, char *argv[])
{
    assert(args == 2 && "process USERNAME"); 
    test(argv);
    return 0;
}